package com.epoint.BookManager.action;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.print.attribute.HashAttributeSet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSONObject;
import com.epoint.BookManager.domain.BookInfo;
import com.epoint.BookManager.domain.BorrowInfo;
import com.epoint.BookManager.service.BookInfoService;
import com.epoint.BookManager.service.BorrowInfoService;





@WebServlet("/borrowinfoaddaction")
public class BorrowInfoAddAction extends HttpServlet
{
    private static final long serialVersionUID = 1L;

    BookInfoService bookInfoService = new BookInfoService();
    BorrowInfoService borrowInfoService =new BorrowInfoService();
    public BorrowInfoAddAction() {
        super();
    }

  
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method");
        if ("querybookinfo".equals(method)) {
            try {
				queryBookInfo(request, response);
			} catch (ParseException e) {
				e.printStackTrace();
			}
        }
        else if ("addborrowinfo".equals(method)) {
            addBorrowinfo(request, response);
        }else if("getBorrowID".equals(method)) {
        	getBorrowId(request,response);
        }else if("getBookname".equals(method)) {
        	
        }
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }



    /**
     * 将输入添加到借阅列表
     * @param request
     * @param response
     * @throws IOException
     * @throws ParseException 
     */
    private void queryBookInfo(HttpServletRequest request,  HttpServletResponse response) throws IOException, ParseException {
    	String data = request.getParameter("data");
		//接收前台的数据封装为BorrowInfo对象
		BorrowInfo borrowInfo = JSONObject.parseObject(data, BorrowInfo.class);
		Map<String,String> resultMap = new HashMap<>();
		boolean flag = borrowInfoService.borrowAdd(borrowInfo);
		if(flag) {
			resultMap.put("msg","借阅成功");
		}else {
			resultMap.put("msg", "借阅失败");
		}
		response.getWriter().write(JSONUtils.toJSONString(resultMap));
    }


    /**
     * 查询可以借阅书的籍的方法
     * 方法名字有错误
     * @param request
     * @param response
     * @throws IOException
     */
    private void addBorrowinfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
    	List<BookInfo> canChanceList = borrowInfoService.getCanChanceList();
        response.getWriter().print(JSONObject.toJSON(canChanceList));
    }
    
    
    /**
           * 获取BorrowId
     * @param request
     * @param response
     * @throws IOException
     */
    public void getBorrowId(HttpServletRequest request, HttpServletResponse response) throws IOException {
    	Calendar cal  =Calendar.getInstance();
    	int year = cal.get(Calendar.YEAR);
		String Id = borrowInfoService.getBorrowId();
		String str = "Borrow"+year+Id;
		response.getWriter().print(str);
	}
    

    
}
